package com.x.erp.controller;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.erp.model.Car;
import com.x.erp.model.CarAuth;
import com.x.erp.model.CarConfInfo;
import com.x.erp.model.CarPrint;
import com.x.erp.rpc.CarAuthRpcService;
import com.x.erp.rpc.CarPrintRpcService;
import com.x.erp.rpc.CarRpcService;
import com.x.erp.util.Constant;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;

/**
 * 价签打印
 * Created by gdf on 2018/1/23.
 */
@RestController
public class CarPrintController extends BaseController {

	@Reference
	private CarPrintRpcService carPrintRpcService;

	@Reference
	private CarRpcService carRpcService;// 车辆服务

	@Reference
	private CarAuthRpcService carAuthRpcService;// 认证证书服务

	/**
	 * 价签配置id
	 */
	int[] confIds = {2001,1004,2002,3002,3001,3003,5002,5003,5004,5005,5006,5007,5008,5009,5012,5013,5014,6001,11005,6003,6004,6005,6006,8001,7001,8003,2003,1002,4002,4003,4001,7002,9003,9002,5011,4004,4005,4007,4006,4008};
	/**
	 * 确认是否可以打印
	 * @param car
	 * @return
	 */
	@RequestMapping(value = "/getPrintCarInfo", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel getPrintCarInfo(Car car) {
		if (car == null || (StringUtils.isEmpty(car.getCarId()) && StringUtils.isEmpty(car.getCarNo()))) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID或车辆编号不能为空");
		}
		car = carRpcService.selectCar(car);
		if(car == null){
			return new ResultModel(ResultCode.RESULT_FAIL, "您的验证码无效，请重新扫码！");
		}
		CarConfInfo carConfInfo = new CarConfInfo();
		carConfInfo.setCarId(car.getCarId());
		List<Map<String, Object>> carConfs = carRpcService.selectCarConfInfo(carConfInfo);
		Map<String, Object> res = new HashMap<>();
		StringBuilder carConfInfoStr = new StringBuilder();
		for(int confId : confIds) {
			for (Map<String, Object> carConf : carConfs) {
				if ((boolean) carConf.get("selectedState") && (int)carConf.get("confId") == confId) {
					carConfInfoStr.append(carConf.get("confName")).append("，");
				}
			}
		}
		if(carConfInfoStr.length() > 0){
			res.put("carConfInfoStr", carConfInfoStr.substring(0, carConfInfoStr.length() - 1));
		}else{
			res.put("carConfInfoStr", carConfInfoStr.toString());
		}
		res.put("car", car);
		// 认证证书信息
		CarAuth carAuth = new CarAuth();
		carAuth.setCarId(car.getCarId());
		carAuth = carAuthRpcService.selectCarAuth(carAuth);
		res.put("carAuth", carAuth);
		CarPrint carPrint = new CarPrint();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		carPrint.setPrintCarId(car.getCarId());
		List<CarPrint> carPrintList = carPrintRpcService.selectCarPrintList(carPrint);
		//如果价签打印次数表不为空
		if (carPrintList != null && carPrintList.size() > 0){
			String printLastTime = formatter.format(carPrintList.get(0).getPrintLastTime());
			String currentTime = formatter.format(System.currentTimeMillis());
			//如果为当日时间且打印次数超过Constant.CAR_PRINT_NUM次则不可以打印
			if (printLastTime.equals(currentTime) && carPrintList.get(0).getPrintTodayNum() >= Constant.CAR_PRINT_NUM){
				res.put("code", 2);
				return new ResultModel(ResultCode.RESULT_FAIL, "您的打印次数已达上限（每天最多" + Constant.CAR_PRINT_NUM + "次），请明日再来打印！", res);
			}
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "可以打印！", res);
	}

	/**
	 * 打印价签修改数据库
	 * @param car
	 * @return
	 */
	@RequestMapping(value = "/carPrint", method = {RequestMethod.POST, RequestMethod.GET})
	public ResultModel carPrint(Car car) {
		if (car == null || StringUtils.isEmpty(car.getCarId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "车辆ID不能为空");
		}
		CarPrint carPrint = new CarPrint();
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		carPrint.setPrintCarId(car.getCarId());
		List<CarPrint> carPrintList = carPrintRpcService.selectCarPrintList(carPrint);
		//如果价签打印次数表不为空
		if (carPrintList != null && carPrintList.size() > 0){
			String printLastTime = formatter.format(carPrintList.get(0).getPrintLastTime());
			String currentTime = formatter.format(System.currentTimeMillis());
			//如果为当日时间
			if (printLastTime.equals(currentTime)){
				//今日次数加1
				carPrint.setPrintTodayNum(carPrintList.get(0).getPrintTodayNum() + 1);
			}else{
				//不是当日时间则设置为1
				carPrint.setPrintTodayNum(1);
			}
			//总打印数量加1
			carPrint.setPrintTotalNum(carPrintList.get(0).getPrintTotalNum() + 1);
			carPrintRpcService.updateCarPrint(carPrint);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "打印成功！");
		}
		//如果为第一次打印则加1
		carPrint.setPrintTotalNum(1);
		carPrint.setPrintTodayNum(1);
		carPrintRpcService.insertCarPrint(carPrint);
		return new ResultModel(ResultCode.RESULT_SUCCESS, "打印成功！");
	}

}
